﻿//
function lib_obj(divN,buttonN,hiddenN,dataArr,pObj,drawTabFN,onclickFN,SysHiddenN){
	this.onclickFN=onclickFN;
	this.div=document.getElementById(divN);
	this.div.builded=false;
	this.drawTabFN=(drawTabFN!=null&&drawTabFN!='')?drawTabFN:'drawTable1';
	try{this.drawTabF=eval(this.drawTabFN)}
	catch(err){this.drawTabF=function(){return 'Error!No drawTable function!'}};
	try{this.drawTab=drawTable;}
	catch(err){this.drawTab=function(){return 'Error!No drawTable function!'}}
	this.divConfig={title:'职位XXX',nCols:2,tableW:500,spaceColW:20,trHeight:24,currentNShowC:100,buttonNShowC:20,AutoName:''};
	this.button=eval(buttonN);
	this.hidden=eval(hiddenN);
	this.SysHidden=eval(SysHiddenN);
	this.preValue=this.hidden.value;
	this.dataArrAll=dataArr;
	this.dataArr=this.dataArrAll;
	this.childNodes=[];
	this.parentObj=null;
	if(pObj) pObj.add(this);
}
lib_obj.prototype.add=function(cNode){
     cNode.parentObj=this;
	 cNode.prePObjV=this.hidden.value;
	 this.childNodes[this.childNodes.length]=cNode;
}
lib_obj.prototype.popup=function(){
	 if(this.parentObj!=null&&(!this.div.builded||this.prePObjV!=this.parentObj.hidden.value)){
	    this.dataArr=[];
		var parentId=null,i;
		for(i=0;i<this.parentObj.dataArr.length;i++) if(this.parentObj.dataArr[i][0]==this.parentObj.hidden.value){parentId=this.parentObj.dataArr[i][0];break;}
		if(parentId!=null) for(i=0;i<this.dataArrAll.length;i++) if(this.dataArrAll[i][1]==parentId) this.dataArr[this.dataArr.length]=this.dataArrAll[i];
		if(!this.dataArr.length) return;
	 }
//	 if(!this.div.builded||(this.parentObj!=null&&this.prePObjV!=this.parentObj.hidden.value)||(this.preValue!=this.hidden.value)){
	     this.div.builded=true;
		 this.preValue=this.hidden.value;
		 if(this.parentObj!=null) this.prePObjV=this.parentObj.hidden.value;
		 this.div.innerHTML=this.buildDiv();
//	 }
	 displayLayer(this.div.id,GetXPos(document.images['img_'+this.div.id]),GetYPos(document.images['img_'+this.div.id])+5);
}
lib_obj.prototype.buildDiv=function(){
     var errorStr='Error!No buildDiv function!';
	 /*try{*/return this.drawTab(this.hidden.value,this.dataArr,this.div.id,this.divConfig,this.onclickFN,this.drawTabF);//}
	 //catch(err){return errorStr;}
}
/******************** onclick function  ********************************/
lib_obj.prototype.passSchCon=function(value,name,id){
	this.hidden.value=value;
	var showName=(GetRealLength(name)<this.divConfig.buttonNShowC)?name:(subStringPro(name,this.divConfig.buttonNShowC-2)+'...');
	this.button.value=showName;
	this.button.title=name;
	if(this.hidden==eval('document.addvacancyForm.JobTypeSelID') || this.hidden==eval('document.addvacancyForm.JobTypeSelID2')){
	//大类选择后，将职位名称与大类传给ajax，返回值存入hidden内
		startRequest(document.addvacancyForm.jobtitle.value,value);
		this.SysHidden.value=Str_SubJobTypeName;
	}else if(value!="" && (this.hidden==eval('document.addvacancyForm.jobnameid') || this.hidden==eval('document.addvacancyForm.jobnameid2'))){
	//小类选择后，将hidden内值清空
		this.SysHidden.value="";
	}
	KeyInOrder();
	ClosePopup(this.div.id,0,0);
	
	if(this.childNodes.length) this.clearAllChildnodes();
	for(var i=0;i<this.childNodes.length;i++) if(this.haveChildnodes(value,this.childNodes[i])) this.childNodes[i].button.disabled=false;else this.childNodes[i].button.disabled=true;
	
	if(this.hidden.name=='JobTypeSelID'&&subjobtypeF) subjobtypeF.popup();
	
	if(window.fngaoduan) window.fngaoduan();
}
lib_obj.prototype.passSchCon4ini=function(value){
     var name,i,flag=false;
	 for(i=0;i<this.dataArr.length;i++) if(this.dataArr[i][0]==value) {name=this.dataArr[i][2];flag=true;break;}
	 if(flag){
	 this.hidden.value=value;
	 var showName=(GetRealLength(name)<this.divConfig.buttonNShowC)?name:(subStringPro(name,this.divConfig.buttonNShowC-2)+'...');
	 this.button.value=showName;
	 this.button.title=name;
	 for(i=0;i<this.childNodes.length;i++) if(this.haveChildnodes(value,this.childNodes[i])) this.childNodes[i].button.disabled=false;else this.childNodes[i].button.disabled=true;}
}
/******************** end onclick function  ********************************/
lib_obj.prototype.clearAllChildnodes=function(){
     if(this.childNodes.length) for(var i=0;i<this.childNodes.length;i++){
	    this.childNodes[i].passSchCon('','请选择职位名称');
	    this.childNodes[i].clearAllChildnodes();
	 }
}
lib_obj.prototype.haveChildnodes=function(pvalue,childObj){
     var flag=false,pid=null,i;
	 for(i=0;i<this.dataArrAll.length;i++) if(this.dataArrAll[i][0]==pvalue) {pid=this.dataArrAll[i][0];break;}
	 if(pid!=null) for(i=0;i<childObj.dataArrAll.length;i++) if(childObj.dataArrAll[i][1]==pid) {flag=true;break;}
	 return flag;
}


/******************** generating HTML  ********************************/
function drawTable(currentV,dataArr,divId,divConfig,onclickFN,drawTabF){
	var tWidth=divConfig.tableW,title=divConfig.title,currentN=null,currentNShow=null,strHTML='',i;
	var autoname="";
	if (divConfig.AutoName!=""){
		autoname=document.getElementsByName(divConfig.AutoName)[0].value;
	}
	for(i=0;i<dataArr.length;i++) if(dataArr[i][0]==currentV) {currentN=dataArr[i][2];break;}
	currentN=(currentN==null)?'不限':currentN;
	currentNShow=(GetRealLength(currentN)<divConfig.currentNShowC)?currentN:(subStringPro(currentN,divConfig.currentNShowC-2)+'...');
	if(autoname==""){
		strHTML += '<table width="'+tWidth+'" border="0" cellpadding="0" cellspacing="0" class="popupDivCon">\
	            <tr><td class="popupDivCon_Title">'+title+'：<span title="'+currentN+'">'+currentNShow+'</span></td><td align="right" class="popupDivCon_Title"><p>\
					[<a class="bluelink" href="javascript:'+onclickFN+'(\'\',\'请选择'+title.substr(title.length-2,2)+'\',\'\');">清除</a>]\
					[<a class="bluelink" href="javascript:ClosePopup(\''+divId+'\',0,0)">关闭</a>]\
				&nbsp;</p></td></tr>\
                <tr><td colspan="2" class="popupDivCon_Content">';
	}else{
	//Show Auto subjobtype Name
		currentN="red12px";
		strHTML += '<table width="'+tWidth+'" border="0" cellpadding="0" cellspacing="0" style="border:#999999 1px solid;">\
	            <tr bgcolor="#e5eaef" height="25"><td>'+title+'&nbsp;&nbsp;&nbsp;&nbsp;建议该'+title+'选择：<a class="bluelink" href="javascript:'+onclickFN+'(\''+autoname.substr(autoname.length-3)+'\',\''+autoname.substr(0,autoname.length-4)+'\',\''+autoname.substr(autoname.length-3)+'\');"><span class="'+currentN+'">'+autoname.substr(0,autoname.length-4)+'</span></a></td><td align="right"><p>[<a class="bluelink" href="javascript:ClosePopup(\''+divId+'\',0,0)">关闭</a>]&nbsp;</p></td></tr>\
                <tr><td colspan="2">';
	}
	strHTML += drawTabF(dataArr,divConfig,onclickFN);
	strHTML += '</td></tr></table>';
	return strHTML;
}

function drawTable1(dataArr,divConfig,onclickFN){
	var tWidth=divConfig.tableW,nCols=divConfig.nCols,spaceColWidth=divConfig.spaceColW,trHeight=divConfig.trHeight;
	var strHTML='',tdWidth = parseInt((tWidth-spaceColWidth*(nCols-1))/nCols);
	strHTML += '<table width="100%" border="0" cellpadding="0" cellspacing="0">';
	//strHTML += '<tr height="24"><td colspan="'+(nCols+1)+'"><a href="#" onClick="'+onclickFN+'(\'\',\'不限\',\'\');return false;" style="font-size:13px;">不限</a></td></tr>';
	strHTML += drawTrs1(dataArr,trHeight,tdWidth,nCols,spaceColWidth,onclickFN);
	strHTML += '</table>';
	return strHTML;
}
function drawTrs1(dataArr,trHeight,tdWidth,nCols,spaceColWidth,onclickFN){
	var strHTML='',indexRow = 0,i,j,fIndex;
	for(i=0;i<dataArr.length;i++){
	    if(dataArr[i] && dataArr[i].length){
		   strHTML += '<tr height="'+trHeight+'">';indexRow++;fIndex=i;
		   for(j=0;j<nCols;j++){
		       i=fIndex+j;
			   if(dataArr[i]) strHTML+='<td width="'+tdWidth+'"><a class="bluelink" href="#" onClick="'+onclickFN+'(\''+dataArr[i][0].replace(/"/g,'&quot;')+'\',\''+dataArr[i][2].replace(/"/g,'&quot;')+'\',\''+dataArr[i][0].replace(/"/g,'&quot;')+'\');return false;">'+dataArr[i][2]+'</a></td>';
		       else strHTML+='<td width="'+tdWidth+'"></td>';
			   if(indexRow==1&&j<(nCols-1)) strHTML+='<td rowspan="99" width="'+spaceColWidth+'" style="border-left:#7B797B 1px dotted;"><img src="http://images.zhaopin.com/new/blank.gif" width="1" height="1" border="0"></td>';
		   }strHTML += '</tr>';
		}else continue;
	}
	return strHTML;
}
/******************** end generating HTML  ********************************/

function GetRealLength(strTemp){
 var i,sum=0;
 for(i=0;i<strTemp.length;i++){if ((strTemp.charCodeAt(i)>=0) && (strTemp.charCodeAt(i)<=255)) sum=sum+1; else sum=sum+2;}
 return sum;
}
function subStringPro(str, length){
  var stri = '';
  for(i=0,j=0; j<length;){
    if((str.charCodeAt(i)>=0) && (str.charCodeAt(i)<=255)){stri+=str.charAt(i);j++;}
	else{stri += str.charAt(i);j += 2;}
    i++;}
  return stri;
}


///////////////////ajax///////////////////
var xmlHttp;
var Str_SubJobTypeName="";
function createXMLHttpRequest(){
	if (window.ActiveXObject){
		xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
	}else{
		if (window.XMLHttpRequest){
			xmlHttp = new XMLHttpRequest();
		}
	}
}
function startRequest(Title,JobType){
	var url = "getSubjobtype.asp?jobtitle="+Title+"&jobtypeid="+JobType;
	Str_SubJobTypeName="";
	if (xmlHttp==null){
		createXMLHttpRequest();
	}
	xmlHttp.onreadystatechange = handleStateChange;
	xmlHttp.open("GET",url,false);
	xmlHttp.send(null);
}
function handleStateChange(){
	if(xmlHttp.readyState ==4){
		if(xmlHttp.status == 200){
			//alert("response:"+xmlHttp.responseText);
			//document.fmmain.companyid.value=xmlHttp.responseText;
			Str_SubJobTypeName=xmlHttp.responseText;
			//document.getElementById("result").innerHTML = xmlHttp.responseText;
		}
		xmlHttp = null;
	}
}

function LoadPageSysJobName(){
	if(document.addvacancyForm.JobTypeSelID.value!="" && document.addvacancyForm.jobnameid.value==""){
		startRequest(document.addvacancyForm.jobtitle.value,document.addvacancyForm.JobTypeSelID.value);
		document.addvacancyForm.Sysjobnameid.value=Str_SubJobTypeName;
	}
	if(document.addvacancyForm.JobTypeSelID2.value!="" && document.addvacancyForm.jobnameid2.value==""){
		startRequest(document.addvacancyForm.jobtitle.value,document.addvacancyForm.JobTypeSelID2.value);
		document.addvacancyForm.Sysjobnameid2.value=Str_SubJobTypeName;
	}
}

///////////////////key in order///////////////////
function KeyInOrder(){
	if(document.addvacancyForm.jobnameid.value!=""){
		document.addvacancyForm.buttonJobType2.disabled=false;	//show 2
	}else{
		//清除第二大类hidden值、显示值
		document.addvacancyForm.JobTypeSelID2.value="";
		document.addvacancyForm.buttonJobType2.value="请选择职位类别";
		//清除第二小类hidden值、显示值
		document.addvacancyForm.jobnameid2.value="";
		document.addvacancyForm.buttonSubJobType2.value="请选择职位名称";
		//清除第二推荐小类名称
		document.addvacancyForm.Sysjobnameid2.value="";
		//disabled第二大类、第二小类按钮
		document.addvacancyForm.buttonJobType2.disabled=true;
		document.addvacancyForm.buttonSubJobType2.disabled=true;
	}
	if(document.addvacancyForm.JobTypeSelID2.value!=""){
//		document.getElementById("OtherJobType").style.display="";	//show other
	}else{
		//清空其他大类
//		document.addvacancyForm.otherJobtypeCache.length=0;
//		document.addvacancyForm.jobcategoryid.value="";
//		document.getElementById('spanOtherJobtype').innerHTML="";
//		document.getElementById("trOtherJobtype").style.display="none"
//		document.getElementById("OtherJobType").style.display="none";	//hidden other
	}
}

function fnPassJobtype(jobtype,e){
	var id='',text='',dIndex='';
	for(var i=0;arrCategory[i];i++) if(arrCategory[i][2]==jobtype){
		id = arrCategory[i][0];
		text = arrCategory[i][2];
		dIndex = i;
		break;
	}
	if(dIndex>-1&&window['jobtypeF']){
		var myEve = e?e:window.event;
		jobtypeF.passSchCon(id,text,id);
	}
}